<?php

namespace app\admin\controller\auth;

use app\common\controller\BaseAdminController;

class Adminlog extends BaseAdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\AdminLog();
    }
    public function index()
    {
        if($this->request->isAjax()){
            list($where, $sort, $order, $offset, $limit) = $this->buildParameter();
//            echo "<pre>";
//            print_r($where);
//            exit;
            $is_tree = $this->request->param('isTree',0);
            if($is_tree){
                $list = $this->model->where($where)
//                    ->where('admin_id',$this->auth->id)
                    ->order('pid','asc')
                    ->order($sort, $order)
                    ->select()->toArray();
//                echo '<pre>';
//                print_r($list);
//                exit;
                $pk = $this->model->getPk();
                $pid_field = $this->request->param('pid_field','pid');
                if(empty($pid_field)){
                    $pid_field = 'pid';
                }
                if(!empty($list)){
                    $min = min(array_column($list,$pid_field));
                    $list = list_to_tree($list,$pk,$pid_field,'children',$min);
                }
                if($this->index_get_data){
                    return [
                        'total' => count($list),
                        'rows' => $list,
                    ];
                }
                return $this->success("获取成功", [
                    'total' => count($list),
                    'rows' => $list,
                ]);
            }else{
                $list = $this->model->where($where)
//                    ->where('admin_id',$this->auth->id)
                    ->order($sort, $order)
                    ->paginate($limit);
            }
            if($this->index_get_data){
                return [
                    'total' => $list->total(),
                    'rows' => $list->items(),
                ];
            }
            return $this->success("获取成功", [
                'total' => $list->total(),
                'rows' => $list->items(),
            ]);
        }
        return $this->view->fetch();
    }
}